// lib/presentation/screens/register_screen.dart
import 'package:basic_ui/localization/string_hardcoded.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../../../routing/app_router.dart';
import '../../data/models/user.dart';
import '../controllers/auth_controller.dart';

class CustomRegisterScreen extends ConsumerStatefulWidget {
  const CustomRegisterScreen({super.key});

  @override
  _CustomRegisterScreenState createState() => _CustomRegisterScreenState();
}

class _CustomRegisterScreenState extends ConsumerState<CustomRegisterScreen> {
  final TextEditingController usernameController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();
  final TextEditingController confirmPasswordController =
      TextEditingController();

  @override
  Widget build(BuildContext context) {
    final authStatus = ref.watch(authControllerProvider);

    return Scaffold(
      appBar: AppBar(title: Text('Register'.hardcoded)),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            TextField(
              controller: usernameController,
              decoration:
                  InputDecoration(labelText: 'Create a Username'.hardcoded),
            ),
            TextField(
              controller: passwordController,
              decoration:
                  InputDecoration(labelText: 'Create a Password'.hardcoded),
              obscureText: true,
            ),
            TextField(
              controller: confirmPasswordController,
              decoration:
                  InputDecoration(labelText: 'Confirm Password'.hardcoded),
              obscureText: true,
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () async {
                if (passwordController.text == confirmPasswordController.text) {
                  final user = User(
                    uid: 'uid123',
                    email: usernameController.text,
                  );
                  await ref
                      .read(authControllerProvider.notifier)
                      .register(user);
                } else {
                  ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                      content: Text('Passwords do not match'.hardcoded)));
                }
              },
              child: authStatus == AuthStatus.loading
                  ? const CircularProgressIndicator()
                  : Text('Create Account'.hardcoded),
            ),
            if (authStatus == AuthStatus.error) ...[
              const SizedBox(height: 20),
              Text('Registration Failed'.hardcoded,
                  style: const TextStyle(color: Colors.red)),
            ],
            TextButton(
              onPressed: () {
                context.goNamed(AppRoute.signIn.name);
              },
              child: Text('Already have an account? Login'.hardcoded),
            ),
          ],
        ),
      ),
    );
  }
}
